﻿using AuroraPlatform.ViewModel;
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AuroraPlatform
{
    public interface IPageProvider
    {
        PageLevel PageLevel { get; }
        INavigate Navigation { get; set; }
    }

    public enum RoleType
    {
        Normal = 1,
        Advanced = 2,
        DB = 3,
        System = 4,
    }


    public class DisplayControler : IValueConverter
    {
        private PageLevel _pageLevel;

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            _pageLevel = (PageLevel)value;
            return IsVisible ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        protected virtual bool IsVisible
        {
            get
            {
                return BusinessContext.Singleton.User != null;
            }
        }

        protected RoleType TargetRoleType
        {
            get
            {
                return (RoleType)Enum.Parse(typeof(RoleType), BusinessContext.Singleton.User.RoleType.ToString(), true);
            }
        }

        protected PageLevel TargetPage
        {
            get
            {
                return _pageLevel;
            }
        }
    }



    public class UserConverter : DisplayControler
    {
        protected override bool IsVisible
        {
            get
            {
                if (base.IsVisible)
                    return TargetRoleType == RoleType.System;
                return false;
            }
        }
    }

    public class ImportConverter : DisplayControler
    {
        protected override bool IsVisible
        {
            get
            {
                if (base.IsVisible)
                    return TargetRoleType == RoleType.System || TargetRoleType == RoleType.DB;
                return false;
            }
        }
    }

    public class ViewConverter : DisplayControler
    {
        protected override bool IsVisible
        {
            get
            {
                if (base.IsVisible)
                    return TargetPage == PageLevel.Fourth && TargetRoleType != RoleType.Normal;
                return false;
            }
        }
    }

    public class EditConverter : ImportConverter
    {
        protected override bool IsVisible
        {
            get
            {
                if (base.IsVisible)
                    return TargetPage == PageLevel.Fourth
                        && (TargetRoleType == RoleType.DB || TargetRoleType == RoleType.System);
                return false;
            }
        }
    }

    public class DeleteConverter : ImportConverter
    {
        protected override bool IsVisible
        {
            get
            {
                if (base.IsVisible)
                    return TargetPage == PageLevel.Fourth;
                return false;
            }
        }
    }

    public class PrintConverter : DisplayControler
    {
        protected override bool IsVisible
        {
            get
            {
                if (base.IsVisible)
                {
                    return TargetPage == PageLevel.Third
                        || TargetPage == PageLevel.Fourth
                        || TargetPage == PageLevel.Fifth;
                }
                return false;
            }
        }
    }

    public class BatchDeleteConverter : DisplayControler
    {
        protected override bool IsVisible
        {
            get
            {
                if (base.IsVisible)
                    return (TargetRoleType == RoleType.DB 
                        || TargetRoleType == RoleType.System) 
                        && TargetPage == PageLevel.Third;
                return false;
            }
        }
    }


    public class SystemUserConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value.ToString() == "system")
                return Visibility.Collapsed;
            return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class WidthConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility visibility = (Visibility)value;
            return visibility == Visibility.Visible ? -40 : 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class VisibleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (bool)value ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (bool)value;
        }
    }


    public class SearchConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value != null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (bool)value;
        }
    }

    public class SignMainConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (BusinessContext.Singleton.User == null
                || BusinessContext.Singleton.User.RoleType != 4)
                return Visibility.Collapsed;

            return (int)value == 1 ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (bool)value;
        }
    }

    public class SignConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (BusinessContext.Singleton.User == null
                || BusinessContext.Singleton.User.RoleType != 4)
                return Visibility.Collapsed;

            return (int)value == 0 ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (bool)value;
        }
    }

    public class SignTextConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return new SolidColorBrush((int)value == 1 ? Colors.Red : Colors.Black);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (bool)value;
        }
    }

    public class SignWeightConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (int)value == 1 ? FontWeights.Bold : FontWeights.Normal;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (bool)value;
        }
    }

    public class LoginConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value == null ? "登    录" : "注    销";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (bool)value;
        }
    }

    public class CartCountConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int intValue = 0;
            if (value != null 
                && int.TryParse(value.ToString(), out intValue) 
                && intValue > 0)
            {
                return intValue.ToString();
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (bool)value;
        }
    }
}
